package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.valid.AddGroup;
import com.sky.vo.OrderPageVO;
import com.sky.vo.OrderSubmitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/*
TODO 有时间补全swagger
 */
@RestController("/userOrderController")
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 用户下单
     * @param dto
     * @return
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody @Validated(AddGroup.class) OrdersSubmitDTO dto) {

        OrderSubmitVO vo = orderService.submit(dto);
        return Result.success(vo);
    }

    /**
     * 历史订单查询
     * @param dto
     * @return
     */
    @GetMapping("/historyOrders")
    public Result history(OrdersPageQueryDTO dto){

        dto.setUserId(BaseContext.getCurrentId());
        PageResult result = orderService.historyOrders(dto);
        return Result.success(result);
    }

    /**
     * 查询订单明细
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    public Result orderInfo(@PathVariable("id") String id){

        OrderPageVO vo = orderService.orderInfo(Long.parseLong(id));
        return Result.success(vo);
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    public Result cancelOrder(@PathVariable("id") String id){

        orderService.cancelOrder(Long.parseLong(id));
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    public Result again(@PathVariable("id") String id){

        OrderSubmitVO again = orderService.again(Long.parseLong(id));
        return Result.success(again);
    }
}
